﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Xml.Linq;

namespace GoogleMapsAPI
{
    public class Data
    {
        public String Group { get; set; }
        public String Name { get; set; }
        public String Lat { get; set; }
        public String Lng { get; set; }

        public static List<XElement> ReadData()
        {
            String dataPath = "Data.xml";

            var data = from dt in XElement.Load(dataPath).Elements("PlaceGroup")
                       select dt;
            return data.ToList();
        }

        public static void InsertPlace(Data d)
        {
            String dataPath = "Data.xml";
            XDocument doc = XDocument.Load(dataPath);



            XElement data = (from dt in doc.Root.Elements("PlaceGroup")
                             where dt.Attribute("name").Value == d.Group
                             select dt).Single();

            var q = from p in data.Elements("Place")
                              where p.Value == d.Name
                              select p;

            if(q.Count() != 0)
                throw new Exception("Duplicate !!!");
            
            XElement place = new XElement("Place", d.Name, new XAttribute("lat", d.Lat), new XAttribute("lng", d.Lng));
            data.Add(place);
            doc.Save(dataPath);
        }

        public static void InsertPlaceGroup(String name)
        {
            String dataPath = "Data.xml";
            XDocument doc = XDocument.Load(dataPath);

            var data = from dt in doc.Root.Elements("PlaceGroup")
                            where dt.Attribute("name").Value == name
                            select dt;

            if (data.Count() != 0)
                throw new Exception("Duplicate !!!");

            XElement placegroup = new XElement("PlaceGroup", new XAttribute("name", name));
            doc.Root.Add(placegroup);
            doc.Save(dataPath);
        }

        public static void DeletePlace(Data d)
        {
            String dataPath = "Data.xml";
            XDocument doc = XDocument.Load(dataPath);
            XElement pgroup = (from dt in doc.Root.Elements("PlaceGroup")
                              where dt.Attribute("name").Value == d.Group
                              select dt).Single();
            XElement place = (from p in pgroup.Elements("Place")
                              where p.Value == d.Name
                              select p).Single();
            place.Remove();
            doc.Save(dataPath);
        }

        public static void DeletePlaceGroup(String name)
        {
            String dataPath = "Data.xml";
            XDocument doc = XDocument.Load(dataPath);
            XElement pgroup = (from dt in doc.Root.Elements("PlaceGroup")
                               where dt.Attribute("name").Value == name
                               select dt).Single();
            pgroup.Remove();
            doc.Save(dataPath);
        }

        public static void UpdatePlaceGroup(String oname, String nname)
        {
            String dataPath = "Data.xml";
            XDocument doc = XDocument.Load(dataPath);
            XElement pgroup = (from dt in doc.Root.Elements("PlaceGroup")
                               where dt.Attribute("name").Value == oname
                               select dt).Single();
            pgroup.Attribute("name").Value = nname;
            doc.Save(dataPath);
        }

        public static void UpdatePlace(Data odata, Data ndata)
        {
            String dataPath = "Data.xml";
            XDocument doc = XDocument.Load(dataPath);
            XElement pgroup = (from dt in doc.Root.Elements("PlaceGroup")
                               where dt.Attribute("name").Value == odata.Group
                               select dt).Single();
            XElement place = (from p in pgroup.Elements("Place")
                              where p.Value == odata.Name
                              select p).Single();

            place.Attribute("lat").Value = ndata.Lat;
            place.Attribute("lng").Value = ndata.Lng;
            place.Value = ndata.Name;
            doc.Save(dataPath);
        }
    }
}
